//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - S+S Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Windows;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.SmartParts;
using Microsoft.Practices.CompositeUI.WPF;
using Microsoft.Practices.ObjectBuilder;

using LitwareHR.Infrastructure.Interface;

namespace LitwareHR.PublicApp.WinUX
{
    /// <summary>
    /// Interaction logic for HeaderView.xaml
    /// </summary>
    [SmartPart]
    public partial class HeaderView
    {
        private HeaderViewPresenter _presenter;
        private IWPFUIElementAdapter _WPFUIElementAdapter;
        private bool _isDisposing;

        /// <summary>
        /// Sets the presenter. The dependency injection system will automatically
        /// create a new presenter for you.
        /// </summary>
        [CreateNew]
        public HeaderViewPresenter Presenter
        {
            set
            {
                _presenter = value;
                _presenter.View = this;
            }
        }

        /// <summary>
        /// Get or set the WPF UI Element adapter service.
        /// </summary>
        [ServiceDependency]
        public IWPFUIElementAdapter WPFUIElementAdapter
        {
            get { return _WPFUIElementAdapter; }
            set { _WPFUIElementAdapter = value; }
        }

        #region IDisposable Members

        /// <summary>
        /// Performs application-defined tasks associated with freeing, 
        /// releasing, or resetting unmanaged resources.
        /// <summary>
        /// <remarks>
        /// See the <see cref="IDisposable"/> interface documentation for
        /// more information. This class uses IDisposable to ensure that the
        /// presenter is properly cleaned up.
        /// </remarks>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        #endregion

        #region Resource Reclamation Support

        ~HeaderView()
        {
            Dispose(false);
        }

        /// <summary>
        /// Returns true if this object has had the Dispose method called.
        /// </summary>
        public bool Disposed
        {
            get { return _presenter == null && _WPFUIElementAdapter == null; }
        }

        /// <summary>
        /// Returns true if this object is in the process of being disposed.
        /// </summary>
        public bool Disposing
        {
            get { return _isDisposing; }
        }

        /// <summary>
        /// Clean up the unmanaged resources used by this object.
        /// </summary>
        /// <remarks>
        /// For this class, this call is important to clean up the
        /// presenter and the underlying UI infrastructure.
        /// </remarks>
        /// <param name="disposing">True if we should clean up managed resources, false if
        /// we should only clean up unmanaged resources.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!Disposed && !_isDisposing)
            {
                _isDisposing = true;
                if (disposing)
                {
                    if (_presenter != null)
                    {
                        _presenter.Dispose();
                        _presenter = null;
                    }

                    if (_WPFUIElementAdapter != null)
                    {
                        System.Windows.Forms.Control host = _WPFUIElementAdapter.Wrap(this);
                        if (!host.Disposing)
                        {
                            host.Dispose();
                        }
                        _WPFUIElementAdapter = null;
                    }
                }
            }
        }

        #endregion

    }
}

